
export function mappingArray(keyMapping, data) {
  if (!keyMapping || !data) {
    return null
  }
  const result = []
  data.forEach(e => result.push(mapping(keyMapping, e)))
  return result
}

export function mapping(keyMapping, obj) {
  if (!keyMapping || !obj) {
    return null
  }
  const keys = Object.keys(keyMapping)
  const result = {}
  keys.forEach(k => {
    result[keyMapping[k]] = obj[k]
  })
  return result
}

export function mappingPiarArray(keyMappingArray, data) {
  console.log(data)
  if (!keyMappingArray || !data) {
    return null
  }
  const result = []
  data.forEach(e => {
    const obj = {}
    keyMappingArray.forEach(keyMapping => {
      const keys = Object.keys(keyMapping)
      keys.forEach(ky => {
        obj[keyMapping[ky]] = e[ky]
      })
    })
    result.push(obj)
  })
  return result
}

export function mappingTreeArray(keyMapping, data, childrenMapping) {
  if (!childrenMapping) {
    childrenMapping = { key: 'children', mapping: 'children' }
  }
  var result = null
  if (data) {
    result = mappingArray(keyMapping, data)
    data.forEach(item => {
      if (item[childrenMapping.key]) {
        for (var i = 0; i < result.length; i++) {
          if (item.id === result[i].id) {
            const children = mappingTreeArray(keyMapping, item[childrenMapping.key], childrenMapping)
            if (children && children.length > 0) {
              result[i][childrenMapping.mapping] = mappingTreeArray(keyMapping, item[childrenMapping.key], childrenMapping)
            }
            break
          }
        }
      }
    })
  }
  return result
}
